<!DOCTYPE html>
<html lang="en">

	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
		<meta name="description" content="">
		<meta name="author" content="">
		<!-- STYLESHEETS -->
		<!--[if lt IE 9]><script src="../../js/flot/excanvas.min.js"></script><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
		<link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
		<link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
		<link rel="stylesheet" type="text/css" href="../../css/responsive.css">

		<link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
		<!-- file-input -->
		<link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css" />
		<!-- SELECT2 -->
		<link rel="stylesheet" type="text/css" href="../../js/select2/select2.min.css" />
		<!-- UNIFORM -->
		<link rel="stylesheet" type="text/css" href="../../js/uniform/css/uniform.default.min.css" />
		<!-- datatable -->
		<link rel="stylesheet" href="../../js/datatables/datatable.css">
		<!-- WIZARD -->
		<link rel="stylesheet" type="text/css" href="../../js/bootstrap-wizard/wizard.css" />
		  <link rel="stylesheet" href="../../js/datepicker/datepicker.css">
		<!-- FONTS 
	<link href='http://fonts.useso.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
-->
		<!-- JQUERY -->
		<script src="../../js/jquery/jquery-2.0.3.min.js"></script>
		<script src="../../js/common.js"></script>
	</head>

	<body>
		<header class="navbar clearfix" id="header">
		</header>

		<!-- PAGE -->
		<section id="page">
			<!-- SIDEBAR -->
			<div id="sidebar" class="sidebar">
				<div class="sidebar-menu nav-collapse">

					<!-- SIDEBAR MENU -->
					<ul>
						
					</ul>
					<!-- /SIDEBAR MENU -->
				</div>

			</div>
			<!-- /SIDEBAR -->
			<div id="main-content">

				<div class="container">
					<div class="row">
						<div id="content" class="col-lg-12">
							<!-- PAGE HEADER-->
							<div class="row">
								<div class="col-sm-12">
									<div class="page-header">
										<!-- STYLER -->

										<!-- /STYLER -->
										<!-- BREADCRUMBS -->
										<ul class="breadcrumb">
											<li>
												<i class="fa fa-home"></i>
												<a href="index.html">首页</a>
											</li>
											<li>
												<a href="#">商品管理</a>
											</li>
											<li>维护商品</li>
										</ul>
										<!-- /BREADCRUMBS -->

									</div>
								</div>
							</div>
							<!-- /PAGE HEADER -->
							<!-- SAMPLE -->
							<form id="form_pack">
							<div class="row col-md-12">
					<div class="box border primary" style="margin-left: 20px;">
										<div class="box-title">
											<h4><i class="fa fa-bars"></i>设置推荐商品 <font color="#AE0000">(设置推荐商品最好满足是“3”的倍数)</font></h4>
										</div>
										<div class="box-body big" style="padding:10px;height:650px">	
										<input type="hidden" name="recommendId" value="" />
										
										<div class="form-group mrg-bt10-right product_list">
											<label class="control-label col-md-3"><span class="required">*</span>适用商品：</label>
											<div class="col-md-8 inline" style="padding-left: 0px;">
											<div class="col-sm-4">
													  <input type="text" class="form-control" name="q" placeholder="输入商品关键字">
													</div>
													<label class="col-sm-1 control-label" style="width: auto">商品款式：</label>
													<div class="col-sm-4">
													<select id="group_select" class="col-md-12 select2-offscreen" tabindex="-1">
															
													</select>		
													</div>													
													<div class="col-sm-1">
									       				<a class="btn btn-primary search">查询</a>
													</div>
											</div>
										</div>
											 
							<div class="col-md-6 product_list">
								<div class="box border red">
									<div class="box-title" style="height: 34px">
									<h6 style="margin-top: 4px;">商品数据</h6>
									</div>
									<div class="box-body" style="max-height: 550px;overflow: auto;">
									<table id="td_product" cellpadding="0" cellspacing="0" border="0" class="datatable table table-striped table-bordered table-hover">
										<thead>
										<tr>
										<th>商品名称</th>
										<th>操作</th>										
										<th class="productCode hide">操作</th>
										</tr>
										</thead>
										<tbody>	
										<tr class="empty"><td colspan=3>请输入条件查询商品</td></tr>									
										</tbody>				
													
									</table>	
									</div>
							     </div>
								 </div>	
								 
								 	<div class="col-md-6 product_list">
								<div class="box border orange">
									<div class="box-title" style="height: 34px">
									<h6 style="margin-top: 4px;">已选择商品</h6>
									</div>
									<div class="box-body" style="max-height: 550px;overflow: auto;">
									<table id="td_selectproduct" cellpadding="0" cellspacing="0" border="0" class="datatable table table-striped table-bordered table-hover">
										<thead>
										<tr>
										<th>商品名称</th>
										<th>操作</th>
										
										<th class="productCode hide">操作</th>
										</tr>
										</thead>
										<tbody>
										<tr class="empty"><td colspan=3>请从左侧添加商品</td></tr>		
										</tbody>		
													
									</table>	
									</div>
							     </div>
								 </div>
											</div>
										</div>
										</div>
										</form>
								<div class="row" style="height: 30px;">
								<div class="col-md-12" style="text-align: center;">
									<div class="box ">
										<a href="#"  class="btn btn-success config bt-submit" style="width: 100px">
											保存
										</a>
									</div>
								</div>
					           </div>	
									</div>
								</div>
										
							
							<!-- /SAMPLE -->

							<div class="footer-tools">
								<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
							</div>
						</div>
						<!-- /CONTENT-->
					</div>
				</div>
			</div>
		</section>
		<!--/PAGE -->
		<!-- JAVASCRIPTS -->
		<!-- Placed at the end of the document so the pages load faster -->

		<!-- BOOTSTRAP -->
		<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>

		<!-- BLOCK UI -->
		<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
		<!--bootbox-->
		<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
		<!-- AUTOSIZE -->
		<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>
		<!-- spinner -->
		<script src="../../js/spinner/spin.js"></script>
		<!-- INPUT MASK -->
		
		<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>
			<!-- UNIFORM -->
	<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
		<!-- DATA TABLES -->
		<script type="text/javascript" src="../../js/datatables/bootstrap-select.js"></script>
		<script type="text/javascript" src="../../js/datatables/datatable.js"></script>
		<script type="text/javascript" src="../../js/datatables/datatable-bootstrap.js"></script>
		<script type="text/javascript" src="../../js/datatables/datatable-tabletools.js"></script>


		<!-- SELECT2 -->
		<script type="text/javascript" src="../../js/select2/select2.min.js"></script>
	
        <script type="text/javascript" src="../../js/datepicker/datepicker.js"></script>
        <script src="../../js/jquery-validate/jquery.validate.min.js"></script>
		<!-- COOKIE -->
		<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
		<!-- CUSTOM SCRIPT -->
		<script src="../../js/script.js"></script>

		<script src="../../js/Sortable.min.js"></script>
		<script>
			var AddRecommend = function() {
			var wizform = $('#form_pack');
			var selectProducts=new Array();//参与活动的商品
			var id=null,flag;
			var $mainDataTable=null;
			
			/**
			 * 保存
			 */
			var saveRecommend=function(){
	            var recommend={};
	               recommend.id=$("[name=recommendId]").val();
	               recommend.groupId=groupId;
	               recommend.productIds=selectProducts.toString();	 
	               if(selectProducts.length==0)
	               {
		                    $.alert("请设置参与活动的商品");
		                    return false;
	               }
	              
	              	               
	               $.blockUI();
	               var url= App.getContextPath() + "admin/recommend/saveProductRecommend.do";
	               if($("[name=recommendId]").val()!="")
	                     url=App.getContextPath() + "admin/recommend/updateProductRecommend.do";
	               
	               $.ajax({
						type: "post",
						url: url,
						data:$.param({recommend:JSON.stringify(recommend)}),
						async: true,
						success: function(result) {
						$.unblockUI();
							if(result.type=="error"){
								$.alert(result.extra);
							}else{
							  location.href="../product/group_list.html?type=Web";
							}
						}						
					});
			};
			
			/**
			 *搜索产品 
			 */
			var searchProduct=function(){
				  var groupIdz=$("#group_select").val();	
	              var url=App.getContextPath()+"admin/product/searchOrdinaryProductList.do?q="+$("input[name=q]").val()+"&isSpecial=true";
	              if(groupIdz!=null&&groupIdz!=-1)
	                  url+="&groupId="+groupIdz;
	              $.ajax({
						type: "post",
						url: url,						
						async: true,
						success: function(result) {	
							
							if(result.data!=null)
							$("#td_product tbody").empty();
							//$("#form_pack").find(".box-body:eq(0)").css("height",result.data.length*34+600);
							$.each(result.data,function(index,item){
								var tr='<tr><td class="productName">'+item.productName+'</td><td><a class="btn btn-warning btn-xs bt-join btn-width"  pstatus="join"><i class="fa fa-plus-square-o" style="margin-right:5px"></i>参与活动</a></td><td class="productCode hide">'+item.id+'</td></tr>';
								$("#td_product tbody").append(tr);
							})
							
						}						
					});
	         };
	         
	         /**
	          *搜索已存在商品 
	          */
	         var getProductRecommend=function(){
		              url=App.getContextPath()+"admin/recommend/getProductById.do?groupId="+groupId;
		              $.ajax({
							type: "post",
							url: url,
							async: true,
							success: function(result) {
								if(result.data!=null)
								$("#td_selectproduct tbody").empty();
								$.each(result.data,function(index,item){
										if($.inArray(item.id,selectProducts)==-1)
								         selectProducts.push(item.id);
										 var tr='<tr><td class="productName">'+item.productName+'</td><td><a class="btn btn-danger btn-xs bt-join btn-width"  pstatus="nojoin"><i class="fa fa-trash-o" style="margin-right:5px"></i>取消参与</a></td><td class="productCode hide">'+item.id+'</td></tr>';
										 $("#td_selectproduct tbody").append(tr);
								})
							}
						});
					    
			          
	         }
	         
	         /**
	          *转义选择 
	          */
	         var groupChange=function(){				
					          App.getGroup();
					          $(".product_list").show();
					          
					           $(".search").unbind("click").click(searchProduct);
					           $(".product_list").off("click").on("click",".bt-join",function(){
					        	   var productName=$(this).closest("tr").find("td:eq(0)").html();
					        	   var productCode=$(this).closest("tr").find("td:eq(2)").html();
							           if($(this).attr("pstatus")=="join"){
							        	   if( $("#td_selectproduct tbody").find(".empty").length==1)$("#td_selectproduct tbody").empty();
							        	   if($.inArray(parseInt(productCode),selectProducts)==-1)
										   {
							        	   //将当前行增加到已选择列表中							        	  
							        	   var tr="<tr><td>"+productName+"</td><td><a class='btn btn-danger btn-xs bt-join btn-width'  pstatus='nojoin'><i class='fa fa-trash-o' style='margin-right:5px'></i>取消参与</a></td><td class='productCode hide'>"+productCode+"</td></tr>";
							        	   $("#td_selectproduct tbody").append(tr);							        	   
							               selectProducts.push($(this).closest("tr").find(".productCode").html());
										   }							        	  
							               $(this).closest("tr").remove();
							               if($("#td_product tbody tr").length==0){
							            	   $("#td_product tbody").append("<tr class='empty'><td colspan=3>没有数据</td></tr>");
							               }
							           }else{
							        	   //将当前行增加到已选择列表中		
							        	   if( $("#td_product tbody").find(".empty").length==1)$("#td_product tbody").empty();
							        	   var tr="<tr><td>"+productName+"</td><td><a class='btn btn-warning btn-xs bt-join btn-width'  pstatus='join'><i class='fa fa-plus-square-o' style='margin-right:5px'></i>参与活动</a></td><td class='productCode hide'>"+productCode+"</td></tr>";
							        	   $("#td_product tbody").append(tr);	
							        	    selectProducts.remove(productCode,selectProducts);
											$(this).closest("tr").remove();	
											if($("#td_selectproduct tbody tr").length==0){
								            	   $("#td_selectproduct tbody").append("<tr class='empty'><td colspan=3>请从左侧选择数据</td></tr>");
								            }
							           }					               
					           
					           });
					     	   
	              };
	       
	         
	         var getRecommendGoods=function(id){
	           $.ajax({
						type: "post",
						url: App.getContextPath() + "admin/recommend/getProductRecommendByGroupId.do?groupId="+id,
						async: true,
						success: function(result) {
						    $("[name=recommendId]").val(result.id);						 			    
							groupChange();
					    }             					
				});
	         };
	       
			
				return {
					init: function() {
					   App.init();
					   groupChange();
					   groupId=$.getUrlParam("groupId");					  
						$(".bt-submit").click(saveRecommend);
						
						if(groupId!=null){getRecommendGoods(groupId);getProductRecommend(groupId);}
					}
				}
			}();
			$(function() {
				$("#header").load("../head.html");
				AddRecommend.init();
			});
		</script>

		<!-- /JAVASCRIPTS -->
	</body>

</html>